home *** CD-ROM | disk | FTP | other *** search
- Path: costello.SanDiegoCA.ATTGIS.COM!dle
- From: dle@costello.SanDiegoCA.ATTGIS.COM (Jake Le)
- Newsgroups: comp.lang.c++,rb.technical
- Subject: Re: Can copy constructor and operator= share code?
- Date: 29 Feb 1996 20:26:29 GMT
- Organization: AT&T Global Information Solutions-San Diego
- Distribution: world
- Message-ID: <4h525l$pja@rap.SanDiegoCA.ATTGIS.COM>
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM>
- NNTP-Posting-Host: costello.sandiegoca.attgis.com
-
- Boris,
-
- You can call the operator= in your copy constructor.
-
- Here is an example:
-
- class CFoo {
-
- public:
- CFoo() { m_iSize = 0; };
- CFoo(const CFoo &foo)
- {
- // Call the operator=
- this->operator=(foo);
- };
-
- CFoo& operator=(const CFoo &foo)
- {
- m_iSize = foo.m_iSize;
- return *this;
- };
-
- void Print() { cout << "Size = " << m_iSize << endl; };
- void SetSize(int size) { m_iSize = size; };
-
- int m_iSize; // Hungarian notation
- };
-
- Hope that help.
- --Jake
-
- In article <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM>, borisb@sd.znet.com (Boris Burtin) writes:
- |> I have noticed that a copy constructor and operator= perform pretty
- |> much the same function. The code I wrote for my class simply copies
- |> each member variable from one class to the other.
- |>
- |> Is there a way for the one of the two functions to call the other, to
- |> avoid duplicate code? I have tried calling the copy constructor from
- |> operator=, but nothing happens. I've gotten around this problem by
- |> creating a private Copy() function, which is called by both the copy
- |> constructor and operator=. Is there another way?
- |>
- |> Thanks,
- |>
- |> Boris
- |>
- |>
-
- --
- \\\======================================================================\\\
- \\\ AT&T Global Information Solutions \\\
- \\\----------------------------------------------------------------------\\\
- \\\ Jake Q. Le || Jake.Le@SanDiegoCA.ATTGIS.COM \\\
- \\\ 17089 Via Del Campo || http://jakepc.SanDiegoCA.ATTGIS.COM \\\
- \\\ San Diego, CA 92127 || \\\
- \\\======================================================================\\\
-